/*
 * Created by JFormDesigner on Wed Jun 07 09:14:07 CST 2023
 */

package gui.admin;

import dao.ProductCategoryDao;
import dao.UserDao;
import entity.ProductCategory;
import entity.User;
import gui.admin.tablemodel.ProductCategoryTableModel;
import gui.admin.tablemodel.UserTableModel;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class ManageProductCategoryFrame extends JFrame {
    public ManageProductCategoryFrame() throws Exception {
        initComponents();
    }

    private void initComponents() throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        scrollPane1 = new JScrollPane();
        table1 = new JTable();
        productCategoryId = new JTextField();
        productCategoryName = new JTextField();
        updateBtn = new JButton();
        deleteBtn = new JButton();
        cancelBtn = new JButton();

        //======== this ========
        setTitle("\u7ba1\u7406\u5546\u54c1\u5206\u7c7b\u4fe1\u606f");
        setPreferredSize(new Dimension(600, 650));
        var contentPane = getContentPane();
        contentPane.setLayout(null);
        this.setVisible(true);

        //---- label1 ----
        label1.setText("\u5546\u54c1\u5206\u7c7b\u7f16\u53f7");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(55, 375, 110, 40);

        //---- label2 ----
        label2.setText("\u5546\u54c1\u5206\u7c7b\u540d\u79f0");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(55, 440, 110, 40);

        //======== scrollPane1 ========
        {

            //---- table1 ----
            table1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
            table1.setModel(new ProductCategoryTableModel());//设置表格的数据模型
            scrollPane1.setViewportView(table1);
        }
        contentPane.add(scrollPane1);
        scrollPane1.setBounds(30, 10, 525, 335);

        //---- productCategoryId ----
        productCategoryId.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(productCategoryId);
        productCategoryId.setBounds(180, 375, 300, 45);

        //---- productCategoryName ----
        productCategoryName.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(productCategoryName);
        productCategoryName.setBounds(180, 440, 300, 45);

        //---- updateBtn ----
        updateBtn.setText("\u4fee\u6539");
        updateBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(updateBtn);
        updateBtn.setBounds(165, 515, 95, 50);

        //给修改按钮添加点击事件
        updateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                String productCategoryIdTemp = productCategoryId.getText();//获取文本输入框的内容
                String productCategoryNameTemp = productCategoryName.getText();

                ProductCategory productCategory = new ProductCategory(productCategoryIdTemp,productCategoryNameTemp);
                ProductCategoryDao productCategoryDao = new ProductCategoryDao();
                boolean result = productCategoryDao.updateProductCategory(productCategory);//调用修改方法
                if(productCategoryId.getText().equals("")){
                    JOptionPane.showMessageDialog(null,"请先选择要修改的商品分类");
                }else{
                    if(result){
                        JOptionPane.showMessageDialog(null,"修改商品分类成功");
                        ProductCategoryTableModel productCategoryTableModel = null;
                        try {
                            productCategoryTableModel = new ProductCategoryTableModel();
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                        table1.setModel(productCategoryTableModel);//刷新表格
                    }else{
                        JOptionPane.showMessageDialog(null,"修改商品分类失败");
                    }
                }
            }
        });

        //---- deleteBtn ----
        deleteBtn.setText("\u5220\u9664");
        deleteBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(deleteBtn);
        deleteBtn.setBounds(300, 515, 95, 50);

        //给删除按钮添加点击事件
        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int res = JOptionPane.showConfirmDialog(null,"你确定要删除该商品分类吗？","删除确认",JOptionPane.YES_NO_OPTION);
                if(res == 0){
                    if (productCategoryId.getText().equals("")){
                        JOptionPane.showMessageDialog(null,"请先选择要删除的商品分类");
                    }else {
                        ProductCategory productCategory = new ProductCategory();
                        productCategory.setCategoryId(productCategoryId.getText());
                        ProductCategoryDao productCategoryDao = new ProductCategoryDao();
                        try {
                            String msgSting = productCategoryDao.deleteProductCategory(productCategory);//调用删除方法
                            JOptionPane.showMessageDialog(null,msgSting);
                            ProductCategoryTableModel productCategoryTableModel = new ProductCategoryTableModel();
                            table1.setModel(productCategoryTableModel);//刷新表格
                        } catch (Exception ex) {
                            throw new RuntimeException(ex);
                        }
                    }
                }
            }
        });

        //---- cancelBtn ----
        cancelBtn.setText("\u53d6\u6d88");
        cancelBtn.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(cancelBtn);
        cancelBtn.setBounds(435, 515, 95, 50);

        cancelBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                dispose();//关闭当前窗口
                try {
                    AdminMainFrame adminMainFrame = new AdminMainFrame();//创建主界面
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });


        //给表格添加鼠标点击事件
        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {

                int row = table1.getSelectedRow();//获取鼠标当前点击的行下标
                //获取点击行商品分类信息
                String productCategoryIdTemp = (String) table1.getValueAt(row,0);
                String productCategoryNameTemp = (String) table1.getValueAt(row,1);
                productCategoryId.setText(productCategoryIdTemp);//设置文本输入框的内容
                productCategoryName.setText(productCategoryNameTemp);
            }
        });

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JScrollPane scrollPane1;
    private JTable table1;
    private JTextField productCategoryId;
    private JTextField productCategoryName;
    private JButton updateBtn;
    private JButton deleteBtn;
    private JButton cancelBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        ManageProductCategoryFrame manageProductCategoryFrame = new ManageProductCategoryFrame();
    }
}
